package com.sise.stuteaintersys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.stuteaintersys.bean.entity.Course;
import com.sise.stuteaintersys.bean.entity.CourseType;
import com.sise.stuteaintersys.mapper.CourseMapper;
import com.sise.stuteaintersys.mapper.CourseTypeMapper;
import com.sise.stuteaintersys.service.AdminCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AdminCourseServiceImpl
 * @Description 管理员课程实现类
 * @Date 2018/12/31 16:50
 * @Version 1.0
 **/
@Service
public class AdminCourseServiceImpl implements AdminCourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CourseTypeMapper courseTypeMapper;

    @Override
    public PageInfo<Course> checkAllCourse(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.selectAllCourse();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Course> checkAll() {
        return new PageInfo<>(courseMapper.selectAllCourse());
    }

    @Override
    public PageInfo<Course> checkCourseByCourseName(Integer pageNum, Integer pageSize, String courseName) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.selectFuzzyCourse(courseName);
        return new PageInfo<>(list);
    }

    @Override
    public List<Course> checkCourseByCourseNameList(Integer pageNum, Integer pageSize, String courseName) {

        return courseMapper.selectFuzzyCourse(courseName);
    }

    @Override
    public PageInfo<Course> checkCourseByCourseTypeName(Integer pageNum, Integer pageSize, String courseTypeName) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.selectCourseByCourseTypeName(courseTypeName);
        PageInfo<Course> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public PageInfo<Course> checkAllCourseByCourseTypeName(String courseTypeName) {
        return new PageInfo<>(courseMapper.selectCourseByCourseTypeName(courseTypeName));
    }

    @Override
    public List<Course> checkCourseAndType(Integer pageNum, Integer pageSize) {
        List<Course> courseList = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<CourseType> types = courseTypeMapper.selectAll();
        PageInfo<CourseType> typePageInfo = new PageInfo<>(types);
        for(int i = 0; i < typePageInfo.getSize(); i++){
            String typeName = typePageInfo.getList().get(i).getTypeName();
            List<Course> list = courseMapper.selectCourseByCourseTypeName(typeName);
            courseList.addAll(list);
        }
        return courseList;
    }

    @Override
    public void delCourse(Course course) {
        courseMapper.deleteByPrimaryKey(course.getId());
    }
}
